//============================================================================= 
#ifndef _CCLIENT_H_ 
    #define _CCLIENT_H_ 

#include <SFML/Network.hpp> 
#include <SFML/Network.hpp>
#include <string> 
#include "Entity.h"
#include "Items.h"
#include "log.h"
//============================================================================= 
class CClient : private sf::Thread{ 
	public:

	CItems itemList;
	// Packet content 
	int packetID; 
	int entityID; 
	int playerID; 
	int command;
	int targX; 
	int targY;
	char name [30];
	char items [5];
	// --------------- 

	sf::SelectorTCP selector; 
	sf::SocketTCP Socket;
	unsigned int NbSockets;
	sf::IPAddress ServerAddress;  

	static CClient& getInstance();

	CClient(); 
	bool Connect(); 
	bool connected;

	void RunClient(); 
	void RunLobby();

	void SendPacket(int packetID, int entityID, int playerID, int command);
	void SendPacket(int packetID, int entityID, int playerID, int command, int tarX, int tarY);
	void SendPacket(int packetID, int entityID, int playerID, int command, int tarX, int tarY,char name[20],char items[5]);

	sf::Packet ReSend;
	sf::Packet ToSend;
	sf::Packet onConnect;

	sf::SocketTCP Client;

	void RunServer();
	int PID;
	sf::SocketTCP Server;			// Server socket, listening for incomming connections.
	sf::Socket::Status status;		// Status of sockets used to check for disconnects.
	

	std::vector<sf::SocketTCP*> Sockets; // Vector of sockets to store all connected sockets.

	bool ServerBroadcastPacket(sf::Packet sendPacket);

	static int myID; 

	bool clientOrServer;

	void DoSomething(bool clientInput) // true Client, False server.
	{
		clientOrServer = clientInput;
		Launch();
	}
private:
	virtual void Run()
	{
		LOGW("Starting thread");
		while(true)
		{
			if(clientOrServer)
				RunClient();
			else
				RunServer();
		}
	};
protected:
	static CClient* client;
}; 

//============================================================================= 

#endif

/*
//============================================================================= 
#ifndef _CCLIENT_H_ 
    #define _CCLIENT_H_ 

#include <SFML/Network.hpp> 
#include <string> 
#include "Entity.h" 

//============================================================================= 
class CClient{ 
public: 

   // Packet content 
   int packetID;   // What type of packet. 
   int entityID;   // EntityID, currently unused. 
   int playerID;   // PlayerID, which player the command is for. 
   int command;   // What command was issued. Move left, cast spell, etc. 
    
   sf::SelectorTCP selector;      // Selector, used to add a timeout on the recieve to not cause blocking. 
   unsigned int NbSockets;         // Used by the Selector. 
    
   CClient();                  // Constructor. 

   // Projectile packets only. 
   int targX;      // Target X coordinate for projectiles. 
   int targY;      // Target Y coordinate for projectiles. 
   // --------------- 
    

   sf::SocketTCP Client;         // The socket used to communicate. 
    
    void RunClient();            // Runs in the gameLoop, gets incoming packets. 
    
   // Send packet function for normal commands.    
   void SendPacket(int packetID, int entityID, int playerID, int command); 
    
   // Send projectile packet function. 
   void SendPacket(int packetID, int entityID, int playerID, int command, int tarX, int tarY); 
    
   sf::Packet ReSend;         // Packet used when receiving packets. 
    
   sf::IPAddress ServerAddress;   // IP Address to connect to. 
   sf::Packet ToSend;            // Packet used when sending packets. 
   void Connect();               // Connect function. Sets up connection. 
   sf::Packet onConnect;         // Packet used when receiving info on connect. 
   static int myID;            // ID of your character. 
   bool connected;               // Connected to the server if true. 

private: 

}; 

//============================================================================= 

#endif*/